import 'package:example_example/pages/view/LoadingView.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import '../../pages/view/LoadingDialog.dart';
import '../../pages/view/LoadingWidge.dart';
import '../../pages/view/NetLoadingDialog.dart';
import '../../services/ScreenAdapter.dart';
import '../../services/TLSizeFit.dart';
import '../view/HeaderView.dart';
import '../../YColors.dart';
import '../view/MyCustomLoadingDialog.dart';

class HeadTest extends StatefulWidget {
  HeadTest({Key key}) : super(key: key);

  HeadTestState createState() => HeadTestState();
}

class HeadTestState extends State<HeadTest> with WidgetsBindingObserver {
  bool flagSel = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  /**
   *
   * //  https://zhuanlan.zhihu.com/p/342130647
      重点是重写 didChangeAppLifecycleState 方法，AppLifecycleState 中的状态包括：resumed、inactive、paused、detached。
   *  状态state:AppLifecycleState.paused
      状态state:AppLifecycleState.resumed
      状态state:AppLifecycleState.inactive

   */
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    // TODO: implement didChangeAppLifecycleState
    super.didChangeAppLifecycleState(state);
    print("状态state:$state");
    if (state == AppLifecycleState.resumed) {
      //TODO
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    ScreenAdapter.init(context);

    return Scaffold(
      appBar: HeaderView(
        title: "看我的头布局",
        isShowReturnArrow: true,
        leading: IconButton(
          icon: Image.asset(
            "images/return_back.png",
            width: 20,
            height: 20,
          ),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        actionList: <Widget>[
          TextButton(
              onPressed: () {
                print("点击了确定");
                if (flagSel) {
                  setSure();
                }
              },
              child: Text(
                "确定",
                style: TextStyle(
                    color: flagSel ? YColors.m : YColors.skill_green,
                    fontSize: 16),
              )),
        ],
      ),
      body: Container(
        width: TLSizeFit.screenWidth,
        color: YColors.t,
        padding: EdgeInsets.fromLTRB(
            TLSizeFit.setPx(20), TLSizeFit.setPx(20), TLSizeFit.setPx(20), 0),
        child: setBodyView(),
      ),
    );
  }

  Widget setBodyView() {
//    return
//      SingleChildScrollView(child:
    return Column(
      children: <Widget>[
        Text("经典款科技"),
        Text("经典款科技"),
        Text("经典款科技"),
        BackButton(
          onPressed: () {
            print("点击了按钮");
          },
        ),

        /**
         * 富文本
         */
        RichText(
          text: TextSpan(
//              style: DefaultTextStyle.of(context).style,
              children: <InlineSpan>[
                TextSpan(
                    text: '老孟',
                    style: TextStyle(
                        color: Colors.red, fontSize: ScreenAdapter.size(32))),
                TextSpan(
                    text: '，',
                    style: TextStyle(
                        color: Colors.red, fontSize: ScreenAdapter.size(32))),
                TextSpan(
                    text: '一个有态度的程序员',
                    style: TextStyle(
                        color: Colors.green, fontSize: ScreenAdapter.size(32))),
                TextSpan(
                    text: '我本将心向明月',
                    style: TextStyle(
                        color: Colors.blue, fontSize: ScreenAdapter.size(30))),
              ]),
        ),
        SizedBox(
          height: 20,
        ),
        Text.rich(
          TextSpan(
              text: '登录即代表同意并阅读',
              style: TextStyle(fontSize: 14, color: Color(0xFF999999)),
              children: [
                TextSpan(
                  text: '《服务协议》',
                  style: TextStyle(
                      color: Colors.blue, fontWeight: FontWeight.bold),
                  recognizer: TapGestureRecognizer()
                    ..onTap = () {
                      print('onTap');
                    },
                ),
              ]),
        ),
        Container(
          margin: EdgeInsets.fromLTRB(0, 30, 0, 20),
          height: 50,
          width: 250,
//          color: Colors.pink,
          padding: EdgeInsets.only(left: 20),
          child: TextField(
//            cursorColor: Colors.red,  // 光标颜色
            style: TextStyle(
                color: Colors.red, fontSize: ScreenAdapter.size(28)), // 字体的样式
            decoration: InputDecoration(
              hintStyle: TextStyle(
                  color: Colors.green, fontSize: ScreenAdapter.size(28)),
              // hint输入提示的样式
              fillColor: Color(0x30cccccc),
//              fillColor: Color(0xffff0000),
              filled: true,
              enabledBorder: OutlineInputBorder(
                  borderSide: BorderSide(color: Color(0x00FF0000)), //0x00FF0000
                  borderRadius: BorderRadius.all(Radius.circular(100))),
              hintText: 'QQ号/手机号/邮箱',
              focusedBorder: OutlineInputBorder(
                  borderSide: BorderSide(color: Color(0x00000000)),
                  borderRadius: BorderRadius.all(Radius.circular(100))),
//              contentPadding: EdgeInsets.symmetric(vertical: 0),  // 上下居中显示
            ),
          ),
        ),

        // 加载的框
        CupertinoActivityIndicator(
          radius: 30,
        ),

//        LoadingDialog(text:""), // text:"卡机"

        RaisedButton(
            child: Text("去加载了啊"),
            onPressed: () {
              setLoadDialog();
            }),

        RaisedButton(
            child: Text("不知为啥只有背景，没有加载框"),
            onPressed: () {
//              new LoadingWidget(visible: true);
              Future.delayed(Duration.zero, () {
                showDialog(
                    context: context,
                    barrierDismissible: true,
                    builder: (_) {
                      return new LoadingWidget(visible: true);
                    });
              });
            }),
//        LoadingWidget(visible: true),

        RaisedButton(
            child: Text("显示加载框"),
            onPressed: () {
              Future.delayed(Duration.zero, () {
                showDialog(
                    context: context,
                    barrierDismissible: true,
                    builder: (_) {
                      return new LoadingDialog(text: "努力加载中");
                    });
              });

              new Future.delayed(Duration(seconds: 4), () {
//                setState(() {
                  Navigator.of(context).pop();
//                });
              });
            }),


//        CustomScrollView(),


        RaisedButton(
            child: Text("最新的加载框了"),
            onPressed: () {
              showDialog(
                  context: context,
                  builder: (context) {
                    return new NetLoadingDialog(
                      dismissDialog: _disMissCallBack,
                      outsideDismiss: false,
                    );
                  }
              );
            }),

        RaisedButton(
            child: Text("也不能用dialog"),
            onPressed: () {
              showDialog(
                  context: context,
                  builder: (context) {
                    return  LoadingView();
                  }
              );
            }),
        LoadingView(),


      ],
//      ),
    );
  }

  setSure() {
    print("点击了确定按钮");
  }

  setLoadDialog() {
    Future.delayed(Duration.zero, () {
      showDialog(
          context: context,
          barrierDismissible: false,
          builder: (_) {
            return MyCustomLoadingDialog();
          });
    });
//  原文链接：https://blog.csdn.net/llq2015/article/details/103586980
  }


  _disMissCallBack(Function func) {
    print("回调了啊_disMissCallBack");

//    new Future.delayed(Duration(seconds: 4), () {
//      Navigator.of(context).pop();
//    });



//    _callBackFunction = (value) {
//      UserEntity entity;
//      if (value is UserEntity)
//        entity = value;
//      if (entity != null && entity.username != null) {
//        showSnackBar("登录成功");
//        Utils.save(Strings.login_name_key, entity.username);
//        Utils.save(Strings.login_state_key, true);
//        new Future.delayed(const Duration(milliseconds: 1500), () =>
//            Navigator.of(context).pop(true));
//      } else {
//        showSnackBar("用户名或密码错误");
//      }
//      func();
//    };
//    _loginViewModel.doLogin(
//        userNameController.text,
//        passwordController.text,
//        _callBackFunction);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    WidgetsBinding.instance.removeObserver(this);
  }
}
